package com.zx.cashiersystem.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import com.zx.cashiersystem.cashiersystem.R;


/**
 * Created by syh on 2016/7/5.
 */
public class Permission {
    private static final String TAG = "Permission";
    //SD卡相关权限
    public static final String[] SD_ARR = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    public static final int SD_CODE = 1;
    //定位相关权限
    public static final String[] LOCATION_ARR = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
    public static final int LOCATION_CODE = 2;
    //定位相关权限
    public static final String[] WIFI_ARR = new String[]{Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.CHANGE_WIFI_MULTICAST_STATE};
    public static final int WIFI_ARR_CODE = 2;

    //相机相关权限
    public static final String[] CAMERA_ARR = new String[]{Manifest.permission.CAMERA};
    public static final int CAMERA_CODE = 3;
    private static final String PACKAGE_URL_SCHEME = "package:"; // 方案

    /**activity中使用
     * 判断是否有权限，没有则申请
     * @param activity
     * @param root
     * @param code
     * @param iRoot
     */
    public static void isHaveRootActivity(Activity activity, String[] root, int code, IRoot iRoot){
        Log.e(TAG, "isHaveRootActivity: ");

        if (lacksPermissions(activity,root)){
            ActivityCompat.requestPermissions(activity,root,
                    code);
        }else {
            iRoot.haveRoot(code);
        }

    }

    /**fragment中使用
     * 判断是否有权限，没有则申请
     * @param fragment
     * @param root
     * @param code
     * @param iRoot
     */
    public static void isHaveRootFragment(Fragment fragment, String[] root, int code, IRoot iRoot){
        Log.e(TAG, "isHaveRootFragment: ");

        if (lacksPermissions(fragment.getContext(),root)){
            fragment.requestPermissions(root,
                    code);
        }else {
            iRoot.haveRoot(code);
        }
    }

    // 判断权限集合
    private static boolean lacksPermissions(Context context, String... permissions) {
        for (String permission : permissions) {
            if (lacksPermission(context,permission)) {
                return true;
            }
        }
        return false;
    }

    // 判断是否缺少权限
    private static boolean lacksPermission(Context context, String permission) {
        return ContextCompat.checkSelfPermission(context, permission) ==
                PackageManager.PERMISSION_DENIED;
    }

    /**
     * 结果接口
     * 如果有有权限直接调用，没有则申请权限，成功后调用
     */
    public interface IRoot {
        void haveRoot(int code);

    }

    private static AlertDialog alertDialog;
    // 显示缺失权限提示
    public static void showMissingPermissionDialog(final Context context) {
        if (alertDialog!=null){
            if (alertDialog.isShowing())
            alertDialog.dismiss();
            alertDialog = null;
        }
        AlertDialog.Builder builder = null;
        if (alertDialog==null) {
            builder = new AlertDialog.Builder(context);
            builder.setTitle(R.string.help);
            builder.setMessage(R.string.string_help_text);

            // 拒绝, 退出应用
            builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                    alertDialog=null;
                }
            });

            builder.setPositiveButton(R.string.setting, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alertDialog.dismiss();
                    alertDialog=null;
                    //listener.isGoToSetting();
                    startAppSettings(context);
                }
            });
            alertDialog = builder.show();
        }


    }
    //dialog点击事件
    public interface DialogClickListener{
        void isQuit();
        //void isGoToSetting();
    }


    // 启动应用的设置
    private static void startAppSettings(Context context) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse(PACKAGE_URL_SCHEME + context.getPackageName()));
        context.startActivity(intent);
    }

}
